VFG.Popup=function(viewer, options) {
	this.viewer = viewer;
	this.options = options;
	this.id = this.options.id|| VFG.Util.getUuid();
	this.position=options.position;
	this.cartesian3=VFG.Util.getLnLaToC3(this.position);
	this.html='<div style="position: absolute;bottom: -16px;border: 8px solid transparent;border-top-color:#353d45;left: 50%;margin-left: -8px;"></div><div id='+this.id+'>'+(this.options.content||"<span>无内容.</span>")+'</div>';
	this.init();
}
VFG.Popup.prototype.init = function() {
	let _this=this;
    layer.open({
    	id:this.id,
        title:this.options.name||false,
        type: this.options.type||1,
        shade:  this.options.shade||0,  
        resize :false,
        closeBtn: 0,
        skin:'layui-layer  layui-layer-city',
	    content: this.html,
	    success: function (layero, dIndex) {
	    	layui.$(layero).children('.layui-layer-content').css('overflow', 'visible');
	    	_this.dIndex=dIndex;
	    	_this.layero=layero;
			_this.width=(layui.$(_this.layero).css('width').replace('px','')*1)/2;
			_this.height=(layui.$(_this.layero).css('height').replace('px','')*1)+16;
			_this.setScreenPositon();
	    	_this.event=function(){
	    		_this.setScreenPositon();
	    	}
	    	_this.viewer.scene.postRender.addEventListener(_this.event);
	    	if(_this.success){
	    		_this.success(layero, dIndex)
	    	}
		},
		end:function(){
	    	if(_this.end){
	    		_this.end(_this.id)
	    	}
	    	_this.destroy();
		}
    }); 
};

VFG.Popup.prototype.setScreenPositon=function(){
	let _this = this;
	var cartesian2=Cesium.SceneTransforms.wgs84ToWindowCoordinates(_this.viewer.scene, _this.cartesian3);
	if (Cesium.defined(cartesian2)){
		layer.style(_this.dIndex, {
			left:(cartesian2.x-_this.width)+'px',
			top:(cartesian2.y-_this.height)+'px'
		});
	}
}

VFG.Popup.prototype.update=function(param){
	let _this = this;
	if(param.content){
		$("#"+this.id).html(param.content);
		_this.width=(layui.$(_this.layero).css('width').replace('px','')*1)/2;
		_this.height=(layui.$(_this.layero).css('height').replace('px','')*1)+16;
	}
	if(param.position){
		_this.position=param.position;
		_this.cartesian3=VFG.Util.getLnLaToC3(_this.position);
		_this.setScreenPositon();
	}
}

VFG.Popup.prototype.on=function(type,callback){
	if('success'===type){
		this.success=callback;
	}
	if('end'===type){
		this.end=callback;
	}
}
VFG.Popup.prototype.destroy = function() {
	var _this = this;
	if(_this.event){
		_this.viewer.scene.postRender.removeEventListener(_this.event);
	}
	
	if(_this.dIndex){layer.close(_this.dIndex);}
	delete this.viewer,
	delete this.option,
	delete this.position,
	delete this.layerIndex;
	delete this.layero;
	delete this.width;
	delete this.height;
	return Cesium.destroyObject(this);
};